<template>
  <div class="my">
    <!-- user信息 -->
    <div class="top">
      <div class="user">
        <div class="left">
          <div class="left-top">{{ userInfo.nickname }}</div>
          <div class="left-bottom">{{ userInfo.intro }}</div>
        </div>
        <img
          class="right"
          :src="userPhoto"
          v-imgerror="avatar"
          @click="editUserInfo"
        />
      </div>
      <ul class="data" v-show="userInfo">
        <li class="item">
          <div class="num">{{ userInfo.submitNum || '0' }}</div>
          <div class="txt">累计答题</div>
        </li>
        <li class="item">
          <div class="num" v-if="userInfo">
            {{ userInfo.collectQuestions.length || '0' }}
          </div>
          <div class="txt">收藏题目</div>
        </li>
        <li class="item">
          <div class="num">{{ userInfo.errorNum || '0' }}</div>
          <div class="txt">我的错题</div>
        </li>
        <li class="item">
          <div class="num">{{ accuracy || '0' }}</div>
          <div class="txt">正确率</div>
        </li>
      </ul>
    </div>

    <!-- 岗位表格 -->
    <div class="job-outside">
      <div class="job" @click="editJob">
        <van-cell :value="userInfo.position" is-link>
          <template #title>
            <i class="iconfont iconicon_mine_gangwei"></i>
            <span class="custom-title">我的岗位</span>
          </template>
        </van-cell>
      </div>
    </div>

    <!-- 面经数据 -->
    <div class="content">
      <div class="middle">
        <h3>面经数据</h3>
        <ul class="data-total">
          <li class="total-item">
            <div class="t1">
              昨日阅读<span class="color" v-if="shareData">{{
                `+${shareData.read.total - shareData.read.yesterday}`
              }}</span>
            </div>
            <div class="num" v-if="shareData">
              {{ userInfo.shareData.read.total }}
            </div>
            <div class="txt">阅读总数</div>
          </li>
          <li class="total-item">
            <div class="t1">
              昨日获赞<span class="color">{{ starAdd }}</span>
            </div>
            <div class="num" v-if="userInfo">
              {{ userInfo.shareData.star.total }}
            </div>
            <div class="txt">获赞总数</div>
          </li>
          <li class="total-item">
            <div class="t1">
              昨日新增<span class="color">{{ commentAdd }}</span>
            </div>
            <div class="num" v-if="userInfo">
              {{ userInfo.shareData.comment.total }}
            </div>
            <div class="txt">评论总数</div>
          </li>
        </ul>
      </div>
    </div>

    <!-- 菜单列表 -->
    <div class="menu">
      <van-cell v-if="userInfo" :value="userInfo.starComments.length" is-link>
        <template #title>
          <i class="iconfont iconicon_mine_mianjing"></i>
          <span class="custom-title">我的面经分享</span>
        </template>
      </van-cell>
      <van-cell v-if="userInfo" :value="messageNum" is-link @click="toMessage">
        <template #title>
          <i class="iconfont iconicon_mine_xiaoxi"></i>
          <span class="custom-title">我的消息</span>
        </template>
      </van-cell>
      <van-cell
        v-if="userInfo"
        :value="userInfo.collectQuestions.length"
        is-link
      >
        <template #title>
          <i class="iconfont iconicon_mine_tikushoucang"></i>
          <span class="custom-title">收藏的题库</span>
        </template>
      </van-cell>
      <van-cell
        v-if="userInfo"
        :value="userInfo.collectArticles.length"
        is-link
      >
        <template #title>
          <i class="iconfont iconicon_mine_xiaoxi"></i>
          <span class="custom-title">收藏的企业</span>
        </template>
      </van-cell>
      <van-cell v-if="userInfo" :value="userInfo.errorQuestions.length" is-link>
        <template #title>
          <i class="iconfont iconicon_mine_tikushoucang"></i>
          <span class="custom-title">我的错题</span>
        </template>
      </van-cell>
      <van-cell v-if="userInfo" :value="userInfo.starArticles.length" is-link>
        <template #title>
          <i class="iconfont iconicon_mine_mianjing"></i>
          <span class="custom-title">收藏的面试经验</span>
        </template>
      </van-cell>
    </div>
    <EditJob
      v-model="userJob"
      :userInfoposition="userInfo.position"
      @refreshPage="refreshPage"
    ></EditJob>
  </div>
</template>

<script>
import { systemMessagesGet, auInfo } from '@/api/my.js'
import EditJob from './edit/editJob.vue'
export default {
  name: 'My',
  components: {
    EditJob
  },
  data () {
    return {
      avatar: require('@/assets/common/avatar.png'),
      userInfo: '',
      shareData: '',
      userJob: false,
      messageNum: '',
      userPhoto: '',
      collectQuestionsLength: ''
    }
  },
  computed: {
    // 准确率计算
    accuracy: function () {
      if ((this.userInfo.submitNum - this.userInfo.errorNum) * 100) {
        return `${Math.round(
          ((this.userInfo.submitNum - this.userInfo.errorNum) /
            this.userInfo.submitNum) *
            100
        )}%`
      } else {
        return '0'
      }
    },
    // 评论数量
    commentAdd: function () {
      if (this.shareData) {
        if (
          // 判断评论总数量是否相比昨天变少了
          this.shareData.comment.total - this.shareData.comment.yesterday >=
          0
        ) {
          return `+${
            this.shareData.comment.total - this.shareData.comment.yesterday
          }`
        } else {
          return this.shareData.comment.total - this.shareData.comment.yesterday
        }
      } else {
        return ''
      }
    },
    // 点赞数计算
    starAdd: function () {
      if (this.shareData) {
        if (
          // 判断点赞总数量是否相比昨天变少了
          this.shareData.star.total - this.shareData.star.yesterday >=
          0
        ) {
          return `+${this.shareData.star.total - this.shareData.star.yesterday}`
        } else {
          return this.shareData.star.total - this.shareData.star.yesterday
        }
      } else {
        return ''
      }
    }
  },
  created () {
    this.$toast.loading({ message: '加载中' })
    this.getInfoList()
  },

  methods: {
    async getInfoList () {
      // 通过token获取用户数据
      const res = await auInfo()
      if (res.data.avatar) {
        const photoUrl = res.data.avatar.slice(1) // 图片返回地址第一个斜杠拼接基地址的时候会导致地址错误
        this.userPhoto = process.env.VUE_APP_URL + photoUrl
      }
      this.userInfo = res.data
      this.shareData = res.data.shareData //  保存数据统计方便下一步的计算

      // 获取消息数
      const messageArr = await systemMessagesGet()
      this.messageNum = messageArr.data.length
      this.$nextTick(() => {
        this.$toast.clear()
      })
    },
    refreshPage () {
      this.getInfoList()
    },
    // 点击头像弹出用户资料框
    editUserInfo () {
      const lastPage = this.$route.fullPath
      this.$router.push({
        path: '/info',
        name: 'Info',
        params: { lastPage: lastPage }
      })
    },
    editJob () {
      this.userJob = true
    },
    toMessage () {
      const lastPage = this.$route.fullPath
      this.$router.push({ name: 'Message', params: { lastPage: lastPage } })
    }
  }
}
</script>

<style lang="less" scoped>
.my {
  overflow-x: hidden;
  background-color: #f7f4f5;
  .top {
    background-color: #c2002e;
    width: 100%;
    padding: 120px 0;
    .user {
      display: flex;
      .left {
        margin-left: 30px;
        flex: 1;
        .left-top {
          height: 59px;
          font-family: PingFangSC-Semibold;
          font-size: 42px;
          font-weight: 600;
          margin-left: 30px;
          color: #ffffff;
          display: block;
        }
        .left-bottom {
          height: 33px;
          opacity: 0.7;
          font-family: PingFangSC-Regular;
          font-size: 24px;
          font-weight: 400;
          margin-left: 30px;
          margin-top: 6px;
          color: rgba(255, 255, 255, 0.7);
        }
      }
      .right {
        margin-right: 74px;
        width: 100px;
        height: 100px;
        border: 6px solid rgba(255, 255, 255, 0.38);
        border-radius: 50%;
      }
    }
    .data {
      padding-top: 50px;
      display: flex;
      justify-content: space-around;
      margin-top: 25px;
      .num {
        height: 59px;
        font-family: PingFangSC-Semibold;
        font-size: 35px;
        color: #ffffff;
        font-weight: 500;
        text-align: center;
      }
      .txt {
        width: 98px;
        height: 33px;
        opacity: 0.7;
        font-family: PingFangSC-Regular;
        font-size: 24px;
        color: #ffffff;
        font-weight: 400;
      }
    }
  }
  .job-outside {
    position: relative;
    left: 30px;
    top: -60px;
    height: 50px;
    .job {
      position: absolute;
      width: 690px;
      border: 1px solid #fff;
      overflow: hidden;
      border-radius: 16px;
      margin: 0 auto;
      text-align: left;
      .iconfont {
        margin-right: 10px;
      }
    }
  }
  .content {
    background-color: #f7f4f5;
    .middle {
      width: 690px;
      height: 330px;
      margin: 0 auto;
      background-color: #fff;
      border-radius: 16px;
      h3 {
        width: 200px;
        height: 80px;
        font-family: PingFangSC-Semibold;
        font-size: 40px;
        color: #181a39;
        text-align: center;
        font-weight: 600;
        padding-top: 50px;
        padding-left: 20px;
      }
      .data-total {
        padding-bottom: 50px;
        margin-top: 80px;
        display: flex;
        justify-content: space-around;
        text-align: center;
        .t1 {
          width: 200px;
          height: 40px;
          font-family: PingFangSC-Regular;
          font-size: 20px;
          color: #b4b4bd;
          text-align: center;
          line-height: 28px;
          font-weight: 400;
          .color {
            width: 50px;
            height: 28px;
            font-family: PingFangSC-Regular;
            font-size: 20px;
            color: #00b8d4;
            letter-spacing: 0.23px;
            text-align: center;
            line-height: 28px;
            font-weight: 400;
          }
        }
        .num {
          margin: 10px auto 10px;
          height: 48px;
          font-family: DINAlternate-Bold;
          font-size: 42px;
          color: #181a39;
          text-align: center;
          font-weight: 700;
        }
        .txt {
          margin: 0 auto 10px;
          width: 98px;
          height: 33px;
          font-family: PingFangSC-Regular;
          font-size: 24px;
          color: #545671;
          text-align: center;
          font-weight: 400;
        }
      }
    }
  }
  .menu {
    width: 690px;
    background: #fff;
    border: 1px solid transparent;
    overflow: hidden;
    border-radius: 16px;
    margin: 10px auto 120px;
    .van-cell__title {
      text-align: left;
    }
  }
}
</style>
